package com.ai.platform.spider.download;

import com.ai.platform.spider.core.SpiderContext;

/**
 * 功能描述
 * 创建者 yeyingsheng
 * 创建时间 2018年06月26日
 */
public abstract class Downloader {
    protected DownloadListener listener;
    protected SpiderContext context;
    public Downloader(SpiderContext context,DownloadListener listener) {
        this.listener=listener;
        this.context=context;
        if(null!=this.listener) {
            this.listener.init(context);
        }
    }

    /**
     * 下载调用
     * @param request
     * @return
     */
    public Response doDownload(Request request){
        if(null!=listener) {
            listener.beforeDownload(context, request);
        }
        Response response=download(request);
        if(null!=listener) {
            listener.afterDownload(context, response);
        }
        return response;
    }

    protected abstract Response download(Request request);
}
